Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array order test case for sortObject #4005

Closed
wants to merge 1 commit into from
Closed

Array order test case for sortObject #4005

wants to merge 1 commit into from

Conversation

swnf
Copy link
Contributor

@swnf swnf commented Oct 2, 2022

This PR contains:

A test case failing in Firefox but not Chrome

Describe the problem you have without this PR

I'm working on a replication library that compares the client's RxSchema hash with the one on the server side. I realized the hash provided by RxDB is not the same across browsers (Chrome, Firefox) if the schema contains an array of objects (an anyOf in my case). This could also be a problem in the future if a browser update changes the browser's implementation of the sort algorithm.

Cause of the issue

src/util.ts has the following lines for sorting arrays:

.sort((a, b) => {
  if (typeof a === 'string' && typeof b === 'string')
    return a.localeCompare(b);

  if (typeof a === 'object') return 1;
  else return -1;
})

It returns 1 if the left operand is an object and -1 otherwise. The spec has no constraints for the order of a and b in the compare function. It seems to be different in Chrome and Firefox.

Possible solutions

  • Return 0 if one operand is not a string (sort is stable, it will preserve the order in this case)
  • Don't sort arrays at all

Both solutions would be a breaking change.

@pubkey
Copy link
Owner

pubkey commented Oct 2, 2022

Thank you for detecting this.
I think we should not sort arrays for schema normalization because that could change the "meaning" of a schema.
The current behavior is dangerous because a schema with a different index (just another order) could result in the same hash because of the normalization.

At the moment RxDB uses these hashes only for the developers security to not accidentially use an existing data store with a different schema without having done a migration. So it is not that big of a problem that the different browsers result in a different hash as long as they create the same hash each time for themself.

However this should be fixed in the next major version, so we should add it to the todo list: https://github.com/pubkey/rxdb/blob/master/orga/before-next-major.md

For your replication plugin you might want to craft your own schema hashes in the meantime.

@swnf
Copy link
Contributor Author

swnf commented Oct 3, 2022

I agree. If you make sure that sortObject recurses into array elements (without sorting the array) the schema hash should be the same in all browsers. Feel free to close this PR until the next major release.

pubkey added a commit that referenced this pull request Oct 3, 2022
@pubkey
Copy link
Owner

pubkey commented Oct 3, 2022

I added this to the todo list.

@pubkey pubkey closed this Oct 3, 2022
pubkey added a commit that referenced this pull request Jan 1, 2023
* CHANGE Do not use hash for revisions

* Remove the deprecated PouchDB RxStorage

* REMOVE pouchdb RxStorage

* FIX typing tests

* FIX imports

* FIX ci

* FIX imports

* FIX tests

* FIX imports

* REMOVe pouchdb from tests

* REMOVE pouchdb scripts

* FIX imports

* FIX import

* FIX tests

* FIX tests

* FIX tests

* FIX lokijs tests

* CHANGE (memory RxStorage) do not clean up database state on closing of the storage, only on `remove()`

* FIX node example

* FIX ci

* FIX stuff

* FIX ci

* FIX foundationdb stuff

* FIX stuff

* FIX test

* FIX t est

* FIX stuff

* REMOVE old `replication-couchdb` plugin. Rename `replication-couchdb-new` to `replication-couchdb`.

* FIX angular replication

* FIX websocket replication

* FIX type

* FIX stuff

* FIX import

* UPDATE stuff

* FIX CouchDB replication: Use correct default fetch method

* FIX vue

* FIX stuff

* FIX stuff

* FIX ci

* FIX vue

* FIX ciu

* aDD log to debug

* FIX stuff

* FIX tests

* CHANGE use plain json errors inside of RxError parameters to make debugging easier.

* FIX tests

* REFACTOR error handling of RxStorageInstance.bulkWrite()

* FIX types

* FIX error

* FIX test

* ADD incremental write queue (#4186)

* ADD incremental write queue

* RMEOVE logs

* FIX types

* Remove depricated `skipIfSame` from `putAttachment()`

* CHANGE immutable documents (#4189)

* CHANGE immutable documents step1

* FIX tests

* FIX more tests

* FIX more tests

* FIX more tests

* FIX more tests

* FIX tests

* FIX tests

* FIX docs

* FIX tests

* FIX tests

* ADD changelog

* FIX tests

* FIX types

* FIX deps

* UPDATE typescript

* UPDATE angular

* UPDATE typescript

* FIX typos

* UPDATE node

* FIX angular

* UPDATE @types/node

* REMOVE unmaintained babel-plugin-transform-async-to-promises

* FIX angular tests

* FIX babel confict

* ADD changelog

* REMOVE comment

* UPDATE testcafe

* TRY fix tests

* FIX run headless

* TRY fix stuff

* TRY ajax timeout

* IMPROVE error log

* FIX stuff

* FIX closing of replication

* FIX todos

* CHANGE `RxCollection.findByIds()` now returns a `RxQuery` (#4191)

* CHANGE `RxCollection.findByIds()` now returns a `RxQuery`

* ADD orga

* FIX do not return deleted documents

* FIX schema hashing should respect the sort order #4005

* FIX lint

* CHANGE Prefix storage plugins with `storage-` like `rxdb/plugins/stor… (#4193)

* CHANGE Prefix storage plugins with `storage-` like `rxdb/plugins/storage-dexie`

* FIX remove more PouchDB stuff

* FIX build

* FIX imports

* FIX types

* FIX imports

* FIX clear cache

* FIX clear cache

* FIX supabase example

* FIX document cache to work with QuickJS

* CHANGE prefix ci task names

* FIX flutter

* FIX tests

* FIX types

* REMOVE more pouchdb specific tests

* REMOVE more pouchdb specific tests

* FIX test

* RENAME atomic->incremental (#4197)

* RENAME atomic->incremental

* ADD more incremental methods

* FIX run hooks on remove

* FIX format

* ADD typings to the query selector

* UPDATE orga

* FIX build

* FIX stuff

* FIX mingo

* FIX circular deps

* TRY fix import

* ADD notes

* REFACTOR utils (#4204)

* REFACTOR utils

* REMOVE $all query type #4203

* Update package.json

* Add missing mingo array operators (#4203)

* ADD #4203 to changelog

* Update dependency babel-loader to v8.3.0 (#4206)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* Update dependency @types/node to v14.18.36 (#4207)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* Update dependency apollo-server-express to v3.11.1 (#4205)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* 13.17.1

* Update dependency firebase to v9.15.0 (#4210)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* Update dependency concurrently to v7.6.0 (#4208)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* REFACTOR use plain functions for replication plugins

* FIX docs

* FIX stuff

* FIX stuff

* FIX build

* TRY fix types

* FIX types

* FIX graphql example

* FIX

* FIX import

Co-authored-by: swnf <50806201+swnf@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* FIX ci

Co-authored-by: swnf <50806201+swnf@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants